package b2.widget.store
{
	import b2.config.*;
	import b2.net.*;
	import b2.util.*;
	import b2.widget.tab.*;
	import b2.widget.vip.ButtonQDian;
	import b2.widget.vip.VipButtonOpen;
	
	import flash.filters.*;
	import flash.text.*;
	
	public class Store extends TabController
	{
		private static var instance:Store;
		
		public function Store()
		{
			var StoreBgr:Class = ResourceHelper.getInstance().getClassByName("StoreBgr");
			var TabTitleBackground:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr6");
			var HighlightTitleBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr3");
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton");
			super(StoreBgr, CloseButton);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
        	var titleFomat:TextFormat = new TextFormat("微软雅黑", 16, 0xFFFFFF);
        	titleFomat.letterSpacing = 10;
			titleText.defaultTextFormat = titleFomat;
			titleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.text = "商店";
			titleText.x = (this.width - titleText.width) / 2;
			titleText.y = 15;
			addChild(titleText);
			var tab:Tab = new Tab("坦克", 3, 2);
			tab.setPageSelectorPos(200, 278);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			tab = new Tab("装备", 3, 2);
			tab.setPageSelectorPos(200, 278);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			tab = new Tab("物品", 3, 2);
			tab.setPageSelectorPos(200, 278);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			tab = new Tab("石油区", 3, 2);
			tab.setPageSelectorPos(200, 278);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			tab = new Tab("荣誉区", 3, 2);
			tab.setPageSelectorPos(200, 278);
			this.addTab(TabTitleBackground, tab.tabName, tab, HighlightTitleBgr);
			//所有物体
			var objectItems:Array = new Array();
			var objectConfig:CommonObjConfig;
			for (var indexStr:String in Config.objectConfigs)
			{
				objectConfig = Config.objectConfigs[indexStr];
				if (objectConfig.showInStore)
				{
					objectItems.push(objectConfig.index);
				}
			}
		    objectItems.sort(sortObjects);
		   
			for (var j:int = 0; j < objectItems.length; j++)
			{
				var index:int = objectItems[j];
				objectConfig = Config.objectConfigs[index];
				var storeItem:StoreItem = new StoreItem(index);
				if (objectConfig.priceGas > 0)
					this.tabs[objectConfig.type].addItem(storeItem, 177, 132);
				else if (objectConfig.priceHonour > 0)
					this.tabs[4].addItem(storeItem, 177, 132);
				else
					this.tabs[3].addItem(storeItem, 177, 132);
			}
			this.setTabTitlePos(26, 51); 
			this.setTabPagePos(24, 145);
			this.setCloseButtonPos(this.width - 44, 16);
			//开通黄钻按钮
			var vipButton:VipButtonOpen = new VipButtonOpen();
			vipButton.x = 24;
			vipButton.y = 105;
			addChild(vipButton);
			//Q点按钮
			var qButton:ButtonQDian = new ButtonQDian();
			qButton.x = 480;
			qButton.y = 105;
			addChild(qButton);
			//黄钻特权文字
			var vipPrivilledgeText:TextField = new TextField();
			vipPrivilledgeText.selectable = false;
			vipPrivilledgeText.autoSize = TextFieldAutoSize.LEFT;
			vipPrivilledgeText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0xFFFFFF, true);
			vipPrivilledgeText.text = "全场Q点商品8折优惠，丰厚黄钻礼包天天拿！";
			vipPrivilledgeText.x = 150;
			vipPrivilledgeText.y = 110;
			addChild(vipPrivilledgeText);
		}

		public static function getInstance():Store
		{
			if (!instance)
				instance = new Store();
			return instance;
		}
		
		public function refresh():void
		{
			for (var i:int = 0; i < this.tabs.length; i++)
			{
				var tab:Tab = this.tabs[i];
				for (var j:int = 0; j < tab.items.length; j++)
				{
					var storeItem:StoreItem = tab.items[j];
					storeItem.refresh();
				}
			}
		}
		
		public function close():void
		{
			if (this.parent != null)
				this.parent.removeChild(this);
		}
		
		//所有物品排序(首先类型降序； 按照requiredLevel升序,然后按照requireInteligenceLevel升序,最后按照priceBones升序)
		private function sortObjects(a:int, b:int):int
		{
			var objectConfigA:CommonObjConfig = Config.objectConfigs[a];
			var objectConfigB:CommonObjConfig = Config.objectConfigs[b];
			//类型降序
			if (objectConfigA.type < objectConfigB.type)
			{
				return 1;
			}
			else if (objectConfigA.type > objectConfigB.type)
			{
				return -1;
			}
			else if (objectConfigA.onSaleFlag && !objectConfigB.onSaleFlag)
			{
				return -1;
			}
			else if (!objectConfigA.onSaleFlag && objectConfigB.onSaleFlag)
			{
				return 1;
			}
			else
			{
				//装备排序
				if (objectConfigA.type == 1 && objectConfigB.type == 1)
				{
					var equipConfigA:EquipConfig = objectConfigA as EquipConfig;
					var equipConfigB:EquipConfig = objectConfigB as EquipConfig;
					if (equipConfigA.suitIndex < equipConfigB.suitIndex)
						return -1;
					else if (equipConfigA.suitIndex > equipConfigB.suitIndex)
						return 1;
					else if (equipConfigA.position < equipConfigB.position)
						return -1;
					else if (equipConfigA.position > equipConfigB.position)
						return 1;
				}
				//坦克排序
				else if (objectConfigA.type == 0 && objectConfigB.type == 0)
				{
					var tankConfigA:TankConfig = objectConfigA as TankConfig;
					var tankConfigB:TankConfig = objectConfigB as TankConfig;
					if (tankConfigA.fightPoint[0] < tankConfigB.fightPoint[0])
						return -1;
					else if (tankConfigA.fightPoint[0] > tankConfigB.fightPoint[0])
						return 1;
				}
//				//需要等级升序	
//				if (objectConfigA.requireLevel < objectConfigB.requireLevel)
//				{
//					return -1;
//				}
//				else if (objectConfigA.requireLevel > objectConfigB.requireLevel)
//				{
//					return 1;
//				}
				//价格升序（天然气）
				if (objectConfigA.priceGas < objectConfigB.priceGas) 
				{
					return -1;
				}
				else if (objectConfigA.priceGas > objectConfigB.priceGas)
				{
					return 1;
				}
				//价格升序（荣誉勋章）
				else if (objectConfigA.priceHonour < objectConfigB.priceHonour) 
				{
					return -1;
				}
				else if (objectConfigA.priceHonour > objectConfigB.priceHonour)
				{
					return 1;
				}
				//价格升序（石油）
				else if (objectConfigA.priceOil < objectConfigB.priceOil) 
				{
					return -1;
				}
				else if (objectConfigA.priceOil > objectConfigB.priceOil)
				{
					return 1;
				}
				//物品索引升序
				else if (objectConfigA.index < objectConfigB.index) 
				{
					return -1;
				}
				else if (objectConfigA.index > objectConfigB.index)
				{
					return 1;
				}
				return 0;
			}
		}
	}
}